package com.neusoft.elmboot.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.neusoft.elmboot.dto.LoginReqDto;
import com.neusoft.elmboot.dto.UserReqDto;
import com.neusoft.elmboot.mapper.UserMapper;
import com.neusoft.elmboot.mapper.UserRoleMapper;
import com.neusoft.elmboot.po.Role;
import com.neusoft.elmboot.po.User;
import com.neusoft.elmboot.po.UserRole;
import com.neusoft.elmboot.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;


@Service
public class UserServiceImpl implements UserService {


    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserRoleMapper userRoleMapper;

    @Override
    public User login(LoginReqDto param) {
        User u = new User();
        u.setUserId(param.getUserId());
        u.setPassword(param.getPwd());
        return userMapper.getUserByIdByPass(u);
    }

    @Override
    public Integer addUser(UserReqDto params) {
        User u = new User();
        BeanUtils.copyProperties(params,u);
        u.setPassword("123456");
        return saveUser(u);
    }

    @Override
    public User getUserByUserId(String id) {
        return userMapper.queryById(id);
    }

    @Override
    public Integer updateUser(UserReqDto params) {
        User u = new User();
        BeanUtils.copyProperties(params,u);
        return userMapper.update(u);
    }

    @Override
    public Integer deleteUser(String id) {
        return userMapper.deleteById(id);
    }

    @Override
    public PageInfo<User> getUserPage(UserReqDto params) {
        PageHelper.startPage(params.getPage(), params.getLimit());
        User u = new User();
        BeanUtils.copyProperties(params,u);
        List<User> users = userMapper.queryAllByLimit(u);
        PageInfo<User> pageInfo = new PageInfo<>(users);
        return pageInfo;
    }

    @Override
    public User getUserByIdByPass(User user) {
        return userMapper.getUserByIdByPass(user);
    }

    @Override
    public int getUserById(String userId) {
        return userMapper.getUserById(userId);
    }

    @Override
    public int saveUser(User user) {
        return userMapper.saveUser(user);
    }


    @Override
    public int assignRole(Map<String, Object> params) {
        UserRole ur = new UserRole();
        ur.setUserId((Integer)params.get("userId"));
        ur.setRoleId(Integer.parseInt(params.get("roleId").toString()));

        // TODO: 另一种解决方法： oldRoleId  ---- newRoleId (前端传入两个角色id，一个旧的，一个新的。)

        // 先删除旧的角色
        userRoleMapper.deleteByPrimaryKey(ur.getUserId());
        // 再增加新的角色
        return userRoleMapper.insert(ur);
    }


    // Ctrl + I  : 实现方法
    @Override
    public Role selectRoleByUserId(Integer userId) {
        return userMapper.selectRoleByUserId(userId);
    }

    @Override
    public List<String> getPermApisByUserId(String userId) {
        return userMapper.selectPermApiByUserId(userId);
    }
}
